//
//  SettingTableViewController.m
//  bitcoin
//
//  Created by caiyajun on 2018/5/8.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "SettingTableViewController.h"
#import "FavsetModel.h"
@interface SettingTableViewController ()
@property (weak, nonatomic) IBOutlet UIButton *sureButton;
@property (weak, nonatomic) IBOutlet UILabel *langageLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *refreshLabel;

@end

@implementation SettingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrowleft-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = back;
    
    
    [self.sureButton setBackgroundImage:[UIImage imageWithColor:RGBColor(86, 54, 157)] forState:(UIControlStateHighlighted)];
}
-(void)viewWillAppear:(BOOL)animated{
    if ([UserDefaults objectForKey:K_LOGININFO_TOKEN]) {
        self.sureButton.hidden=NO;
        [self requstData];
    }else{
        self.sureButton.hidden=YES;
        [UserDefaults setObject:@"zh-Hans" forKey:@"appLanguage"];
        self.langageLabel.text=@"简体中文";
        [UserDefaults setObject:@"3" forKey:K_PRICE];
        self.priceLabel.text=@"澳元";
        [UserDefaults setObject:@"1" forKey:K_REFRESH];
        self.refreshLabel.text=@"始终实时行情";
        [UserDefaults synchronize];
    }
}
-(void)requstData{
    
    [LSRequestManager getRequestWithURL:HUOBI(@"api/user/favSet")
                                  params:nil
                                 success:^(NSDictionary *responseData) {
                                     if ([responseData[@"ret"] integerValue ] == 0) {
                                         FavsetModel*favsetModel = [FavsetModel mj_objectWithKeyValues:responseData[@"data"]];
                                         
                                         
                                         
                                         
                                         if ([favsetModel.lang isEqualToString:@"1"]) {
                                             self.langageLabel.text=@"简体中文";
                                             [UserDefaults setObject:@"zh-Hans" forKey:@"appLanguage"];
                                         }else{
                                             self.langageLabel.text=@"English";
                                             [UserDefaults setObject:@"en" forKey:@"appLanguage"];
                                         }
                                         if ([favsetModel.price_mod isEqualToString:@"1"]) {
                                             self.priceLabel.text=@"人民币";
                                         }else if ([favsetModel.price_mod isEqualToString:@"2"]){
                                             self.priceLabel.text=@"美元";
                                         }else if ([favsetModel.price_mod isEqualToString:@"3"]){
                                             self.priceLabel.text=@"澳元";
                                         }
                                         if ([favsetModel.refresh isEqualToString:@"1"]) {
                                             self.refreshLabel.text=@"始终实时行情";
                                         }else if ([favsetModel.refresh isEqualToString:@"2"]){
                                             self.refreshLabel.text=@"始终普通行情";
                                         }else if ([favsetModel.refresh isEqualToString:@"3"]){
                                             self.refreshLabel.text=@"仅在WEFI下实时行情";
                                         }
                                         
                                         [UserDefaults setObject:favsetModel.price_mod forKey:K_PRICE];
                                         [UserDefaults setObject:favsetModel.refresh forKey:K_REFRESH];
                                         [UserDefaults synchronize];
                                         
                                         
                                         
                                         
                                     } else {
                                         [MBProgressHUD showError:responseData[@"msg"]];
                                     }
                                 }
                                 failure:^(NSError *error) {
                                     
                                 }
                                 showHUD:NO];
}


- (void)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 6;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

{
    
    if (indexPath.row==0) {
        [self performSegueWithIdentifier:@"languageSegue" sender:nil];
    }else if (indexPath.row==1){
        [self performSegueWithIdentifier:@"moneyKindSegue" sender:nil];
    }else if (indexPath.row==2){
        [self performSegueWithIdentifier:@"marketRefreshSegue" sender:nil];
    }else if (indexPath.row==3){
        [self performSegueWithIdentifier:@"helpSegue" sender:nil];
    }
    
    
    
}
- (IBAction)leaveAction:(UIButton *)sender {
    
    
    
    [LSRequestManager postRequestWithURL:HUOBI(@"api/auth/singout")
                                  params:nil
                                 success:^(NSDictionary *responseData) {
                                     if ([responseData[@"ret"] integerValue ] == 0) {
                                         [MBProgressHUD showSuccess:responseData[@"msg"]];
                                         [UserDefaults removeObjectForKey:K_LOGININFO_USERNAME];
                                         [UserDefaults removeObjectForKey:K_LOGININFO_PASSWORD];
                                         [UserDefaults removeObjectForKey:K_LOGININFO_TOKEN];
                                         [UserDefaults removeObjectForKey:K_ISEMAIL];
                                         [UserDefaults removeObjectForKey:K_ISGOOGLE];
                                         [UserDefaults removeObjectForKey:K_MOBIL];
                                         [UserDefaults removeObjectForKey:K_EMAIL];
                                         [UserDefaults synchronize];
                                         
                                         
                                         
                                         
                                         self.sureButton.hidden=YES;
                                     } else {
                                         [MBProgressHUD showError:responseData[@"msg"]];
                                     }
                                 }
                                 failure:^(NSError *error) {
                                     
                                 }
                                 showHUD:NO];
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}


@end
